package com.teamone.etl.aspect;

import cn.hutool.core.date.DateUtil;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.context.annotation.Configuration;

/**
 @author WangZhuoRan */
@Aspect
@Configuration
public class GlobalControllerAspect {

    @Resource
    private HttpServletResponse response;

    @Resource
    private HttpServletRequest request;

    @Around("execution(* com.teamone.etl.*.controller.*.*(..)) " +
            "&& (@annotation(org.springframework.web.bind.annotation.GetMapping) " +
            "|| @annotation(org.springframework.web.bind.annotation.PostMapping) " +
            "|| @annotation(org.springframework.web.bind.annotation.DeleteMapping) " +
            "|| @annotation(org.springframework.web.bind.annotation.PutMapping) " +
            "|| @annotation(org.springframework.web.bind.annotation.RequestMapping))")
    public Object adminControllerBeforeValidation(ProceedingJoinPoint joinPoint) throws Throwable {
        //获取时间
        Date now = new Date();
        String date = DateUtil.year(now)
                +"-"+(DateUtil.month(now) + 1)
                +"-"+DateUtil.dayOfMonth(now);
        String time = DateUtil.hour(now, true)
                +":"+DateUtil.minute(now)
                +":"+DateUtil.second(now)
                +"."+DateUtil.millisecond(now);
        /*
         * 截取信息
         */
        Signature signature = joinPoint.getSignature();
        String longString = signature.toLongString();
        String resultType = longString.substring(0, longString.lastIndexOf(" "));
        String half1 = longString.substring(longString.lastIndexOf(" ")+1,longString.indexOf("("));
        String half2 = longString.substring(longString.indexOf("("));
        String moduleName = half1.substring(half1.indexOf("etl."), half1.indexOf(".controller"));
        File file = new File(System.getProperty("user.dir")+"\\logs\\"+date+"_"+moduleName+".log");
        if (!file.exists()) {
            file.createNewFile();
        }
        try(BufferedWriter bw = new BufferedWriter(new FileWriter(file,true))) {
            bw.write(time+"----------------------------------------\n");
            bw.write("URL:"+request.getRequestURL().toString()+"\n");
            bw.write("HTTPMethod:"+request.getMethod()+"\n");
            bw.write("IP:"+request.getRemoteAddr()+"\n");
            bw.write("ResultType:"+resultType+"\n");
            bw.write("Package-Class-Method:"+half1+"\n");
            bw.write("Parameters:"+half2+"\n");
            bw.write("           "+Arrays.toString(joinPoint.getArgs())+"\n\n");
            bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return joinPoint.proceed();
    }

}
